package io.renren.common.utils.excel;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/10/31
 * Time: 17:03
 * To change this template use File | Settings | File Templates.
 * Description:
 */
public abstract class ExcelDataCreater<T> {


    private  ExecutorService pool;
    private final   int MAX=50000;

    private  int threads=3;

    abstract public List<T>  queryDataForExcel(Map<String,Object> params);

    abstract public int queryTotalForExcel(Map<String,Object> params);


    public final ExecutorService getPool(Integer total) {
        int i = getThreads(total);
        setThreads(i);
        this.pool = Executors.newFixedThreadPool(i);
        return pool;
    }

    public final void setThreads(int threads) {
        this.threads = threads;
    }

    public final int getThreads() {
        return threads;
    }

    private final int getThreads(int total){

        int availableProcessors = Runtime.getRuntime().availableProcessors();
        if(total < MAX){
            return 1;
        }
        int i = total / MAX;
        if(i>availableProcessors){
            return threads > availableProcessors?1:threads;
        }else{
            return i;
        }
    }

}
